package com.meviewer.service;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.location.Location;
import android.location.LocationListener;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;

import com.meviewer.common.FileUtil;
import com.meviewer.common.JSON;
import com.meviewer.common.Registry;
import com.meviewer.common.Setting;

import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Date;
import java.util.Queue;

import static androidx.core.app.ActivityCompat.startActivityForResult;

public class GPSServiceWrapper {

    private static GPSServiceWrapper __ginstance = null;
    public static GPSServiceWrapper getInstance(){
        if(__ginstance == null){
            __ginstance = new GPSServiceWrapper();
        }
        return __ginstance;
    }

    boolean mBound = false;
    private ServiceConnection mConnection = null;

    public boolean getServiceStatus(){
        return mConnection != null;
    }

    public boolean setServiceEnabled(int sec){

       /* System.out.println("22222222");*/

        if(sec == -1){
          /*  System.out.println("33333333333");*/
            return this.stopService();
        }
        else{
            /*System.out.println("4444444444444444");*/
            this.stopService();
            Setting.GPS_RECORD_STEP_MS = sec * 1000;
            return this.startService();
        }
    }

    public boolean startService(){
        if(mConnection != null) return true;
        mConnection = new ServiceConnection() {
            public void onServiceConnected(ComponentName className, IBinder service) {
                gpsListener = new GPSListener();
                GPSService.getInstance().addGPSListener(gpsListener);
                mBound = true;
            }
            public void onServiceDisconnected(ComponentName className) {
                gpsListener = null;
                mBound = false;
            }
        };
        Context c = Registry.getInstance().getApplicationContext();
        if(c != null){
            boolean res =c.bindService(new Intent(c, GPSService.class), mConnection, Context.BIND_AUTO_CREATE);
            if(!res){
                mConnection = null;
            }
            return res;
        }
        return false;
    }

    public void setGPSRecordEnable(boolean b){
        Setting.GPS_RECORD_ENABLE = b;
    }

    public boolean stopService(){
        Context c = Registry.getInstance().getApplicationContext();
        if(c != null && mConnection != null){
            c.unbindService(mConnection);
        }
        gpsListener = null;
        mBound = false;
        mConnection = null;
        return true;
    }

    public class LocJson {
        public Long time;
        public double lon;
        public double lat;
        //public double hei;
    }

    private GPSListener gpsListener = null;
    private class GPSListener implements LocationListener {

        GPSListener(){
            if(locBuffer == null){
                locBuffer = new ArrayList<LocJson>();
            }
            if(locList == null){
                locList = new ArrayList<LocJson>();
            }
        }


        public ArrayList<LocJson> locList = null;
        public ArrayList<LocJson> locBuffer = null;
        public LocJson currentLoc = null;
        public boolean recordGPSFlag = true;
        @Override
        public void onLocationChanged(Location l) {//定位改变监听
            //writeGPSInfo(location);
            //locQueue.add(location);

            LocJson lj = new LocJson();
            lj.time = l.getTime();
            lj.lon = l.getLongitude();
            lj.lat = l.getLatitude();
            //lj.hei = l.get
            locBuffer.add(lj);
            if(Setting.GPS_RECORD_ENABLE){
                locList.add(lj);
            }

            currentLoc = lj;
        }



        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) {//定位状态监听

        }

        @Override
        public void onProviderEnabled(String provider) {//定位状态可用监听

        }

        @Override
        public void onProviderDisabled(String provider) {//定位状态不可用监听

        }
    }


    public String getChangedGpsData(){
        if(gpsListener == null){
            return "[]";
        }
        String str = JSON.stringify(gpsListener.locBuffer);
        gpsListener.locBuffer.clear();
        Log.d("GPS",str);
        return str;
    }

    public String getCurrentGPS(){
        if(gpsListener == null){
            return "";
        }
        if(gpsListener.currentLoc == null){
            return "";
        }
        String str = JSON.stringify(gpsListener.currentLoc);
        /*System.out.println(new Date());
        System.out.println("就撒了带来的");
        System.out.println(str);*/
        return str;
    }

    public String writeGPStoFile(){
        String str = JSON.stringify(gpsListener.locList);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
        Date dt = new Date();
        String timestr = simpleDateFormat.format(dt);
        String filename = Registry.getInstance().getAppStoragePath() + "GPS/";
        FileUtil.CreateDir(filename);
        filename = filename + timestr + ".gps";
        if(FileUtil.WriteTxtFile(str,filename)){
            gpsListener.locList.clear();
            return filename;
        }
        return "";
    }

    public String ReadGPSFile(String path){
        return FileUtil.ReadTxtFile(path);
    }
}
